package com.wzzc.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wzzc.bean.DataTableMsg;
import com.wzzc.bean.Msg;
import com.wzzc.bean.Student;
import com.wzzc.service.UserService;
import com.wzzc.utils.DockerUtils;
import com.wzzc.utils.MyUtils;

@Controller
public class UserController {
	
	public static final String SESSION_USER="SESSION_USER";
	
	@Autowired
	private UserService userService;
	
	/**
	 * 用户登录
	 * @param student
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/login")
	public Msg login(Student student,HttpServletRequest request) {
		request.getSession().invalidate();
		String username=student.getStudentNo();
		String password=student.getStudentPassword();
		if (username.equals("admin")&&password.equals("admin")) {
			Student admin=new Student();
			admin.setStudentNo(username);
			admin.setStudentPassword(password);
			admin.setStudentRealname("admin");
			request.getSession().setAttribute(SESSION_USER, admin);
			return Msg.success("登录成功").putData("target", "admin");
		}else {
			Student user = userService.login(username, password);
			if (user!=null) {
				if (user.getDockerState()==0||user.getDockerState().equals("0")) {
					request.getSession().setAttribute(SESSION_USER, user);
					return Msg.success("登录成功").putData("target", "index");
				}else {
					return Msg.fail("当前用户正在使用中,请稍后重试！").putData("target", "index");
				}
			}else {
				return Msg.fail("账号或密码错误").putData("target", "index");
			}
		}
	}
	
	@RequestMapping("/loginOut")
	public String loginOut(HttpServletRequest request) {
		request.getSession().invalidate();
		return "redirect:/login.jsp";
	}
	
	/**
	 * 后台管理界面
	 * @return
	 */
	@RequestMapping("/admin")
	public String adminUI() {
		return "admin";
	}
	
	/**
	 * 用户主页界面
	 * @return
	 */
	@RequestMapping("/index")
	public String indexUI() {
		return "index";
	}
	
	/**
	 * 学生导入界面
	 * @return
	 */
	@RequestMapping("/importUI")
	public String studentImportUI() {
		return "import";
	}
	
	
	/**
	 * 班级查询界面
	 */
	@RequestMapping("/classInfoUI")
	public ModelAndView classInfoUI(@RequestParam("className")String className) {
		ModelAndView modelAndView=new ModelAndView("classInfo");
		modelAndView.addObject("className",className);
		return modelAndView;	
	}
	
	/**
	 * 查询当前班级学生数据
	 */
	@ResponseBody
	@RequestMapping("/getStudentData")
	public DataTableMsg getStudentData(@RequestParam("className")String className,
			@RequestParam(value="page",defaultValue="1")int page,
			@RequestParam(value="limit",defaultValue="10")int limit) {
		PageHelper.startPage(page, limit);
		List<Student> list = userService.selectStudenByClassname(className);
		PageInfo<Student> pageInfo=new PageInfo<Student>(list);
		DataTableMsg dataTableMsg=new DataTableMsg(0, "success", pageInfo.getTotal(), pageInfo.getList());
		return dataTableMsg;
	}
	
	/**
	 * 关闭Docker镜像
	 */
	@ResponseBody
	@RequestMapping("/dockerCloseByName")
	public Msg dockerCloseByName(@RequestParam("name")String name,@RequestParam("uiid")String uiid) {
		try {
			Student student=new Student();
			student.setDockerState(0);
			userService.updateStudentDockerState(uiid, student);
			MyUtils.dockerImagesMap.remove(uiid);
			DockerUtils.closeDocker(name);
		} catch (Exception e) {
			e.printStackTrace();
			return Msg.fail(null);
		}
		return Msg.success(null);
	}
	
	/**
	 * 关闭当前Session用户Docker镜像
	 */
	@RequestMapping("/dockerCloseCurrent")
	public String dockerCloseCurrent(HttpServletRequest request) {
		Student student = (Student) request.getSession().getAttribute(SESSION_USER);
		return "forward:dockerCloseByName?name="+student.getStudentNo()+"&uiid="+student.getDockerUuid();
	}
	/**
	 * 关闭当前班级所有Docker镜像
	 */
	@ResponseBody
	@RequestMapping("/dockerCloseClass")
	public Msg dockerCloseClass(@RequestParam("className")String className) {
		boolean closeResult = userService.updateDockerStateByClassname(className);
		if (closeResult) {
			return Msg.success(null);
		}
		return Msg.fail(null);
	}
	
	/**
	 * 关闭全部Docker镜像
	 */
	@ResponseBody
	@RequestMapping("/dockerCloseAll")
	public Msg dockerCloseAll() {
		try {
			userService.updateDockerStateCloseALL();
			DockerUtils.closeAllDocker();
		} catch (Exception e) {
			e.printStackTrace();
			return Msg.fail(null);
		}
		return Msg.success(null);
	}

}
